/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.sql.Time;
import java.util.List;

import net.sf.ehcache.ObjectExistsException;

import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.ShiftDAO;
import com.itinsys.enma.domain.PriorityEnvironment;
import com.itinsys.enma.domain.Shift;
import com.itinsys.enma.domain.response.ShiftResponse;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.ShiftManager;

/**
 * @author pferraz
 *
 */
public class ShiftManagerImpl implements ShiftManager{

	@Autowired
	private ShiftDAO shiftDAO;
	@Autowired
	private ResponseManager responseManager;
	
	@Override
	public ShiftResponse getAllShifts() {
		ShiftResponse response=new ShiftResponse();
		List<Shift> shifts=shiftDAO.getAll();
		if(shifts.isEmpty())
			responseManager.prepareResponse(response, ShiftResponse.SHIFTS_NOT_FOUND);
		else{
			responseManager.prepareResponse(response, ShiftResponse.OK);
			response.setShifts(shifts);
		}
		return response;
	}

	@Override
	public Shift getShiftByHourRange(Time startHour, Time endHour) {
		
		return shiftDAO.getShiftByHourRange(startHour,endHour);
	}

	@Override
	public ShiftResponse addShift(Shift shift) {
		ShiftResponse response=new ShiftResponse();
		
		if(validateShift(response,shift,true)){
			if(shiftDAO.addShift(shift))		
				response=getAllShifts();
			else
				responseManager.prepareResponse(response, ShiftResponse.ERROR);
		}
		
		return response;
	}

	@Override
	public ShiftResponse updateShift(Shift shift) {
		ShiftResponse response=new ShiftResponse();
		Shift shiftToUpdate = shiftDAO.getById(String.valueOf(shift.getId()));
		boolean changeDesc=!shift.getDescription().equalsIgnoreCase(shiftToUpdate.getDescription());
		
		if(validateShift(response,shift,changeDesc)){
			shiftToUpdate.setDescription(shift.getDescription());
			shiftToUpdate.setStartTime(shift.getStartTime());
			shiftToUpdate.setEndTime(shift.getEndTime());
			if(shiftDAO.updateShift(shiftToUpdate))		
				response=getAllShifts();
			else
				responseManager.prepareResponse(response, ShiftResponse.ERROR);
		}
		return response;
	}

	private boolean validateShift(ShiftResponse shtResp, Shift shift, boolean changeDesc) {
		try {
			if(changeDesc){
				shiftDAO.getByDesc(shift.getDescription());
				this.responseManager.prepareResponse(shtResp,ShiftResponse.SHIFT_EXISTS);
				return false;
			}
		} catch (ObjectExistsException e1) {
		}
		//Si un turno esta dentro de otro u otros, si ya esta abarcado ese horario
		if(!shiftDAO.validateShiftRange(shift.getId(),shift.getStartTime(),shift.getEndTime())){
			this.responseManager.prepareResponse(shtResp,ShiftResponse.SHIFT_RANGE_EXISTS);
			return false;
		}

		return true;
	}
	
	@Override
	public ShiftResponse deleteShift(Shift shift) {
		ShiftResponse response=new ShiftResponse();
		
		List<PriorityEnvironment> priorityEnvironments=shiftDAO.getPriorityEnvironmentByShifts(shift.getId());
				
		if(priorityEnvironments.isEmpty()){
			shift=shiftDAO.getById(String.valueOf(shift.getId()));
			if(shiftDAO.deleteShift(shift))		
				response=getAllShifts();
			else
				responseManager.prepareResponse(response, ShiftResponse.ERROR);
		}else{
			responseManager.prepareResponse(response,ShiftResponse.SHIFT_HAVE_PRIORITY);
		}		
		
		return response;
	}

	

}
